package com.keta.generator.db;

import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.keta.generator.util.Resources;
import com.keta.generator.vo.Column;
import com.keta.generator.vo.Table;


/**
 * Oralce Metadata读取
 * 2013-6-19
 */
public class Oracle extends DataSource {

	public Oracle(){

		Properties props =new Properties();

		props.put("remarksReporting","true");
		props.put("user",Resources.JDBC_USERNAME);
		props.put("password",Resources.JDBC_USERNAME);

		try {
			Class.forName(Resources.JDBC_DRIVER).newInstance();
			conn = DriverManager.getConnection(Resources.JDBC_URL,props);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 获取所有列
	 * 
	 * @param rs
	 * @param t
	 * @throws SQLException
	 */
	protected void initColumns(Table table) throws SQLException {

		DatabaseMetaData metaDdate = conn.getMetaData();
		ResultSet rs = metaDdate.getColumns(null, "", table.getTableName(), "");
		table.setColumns(new ArrayList<Column>());
		while (rs.next()) {
			// 这里没有提供获取当前列是否主键/外键的信息提示
			Column col = new Column();
			col.setName(rs.getString("COLUMN_NAME"));
			col.setSqlType(rs.getString("TYPE_NAME"));
			col.setSize(rs.getInt("COLUMN_SIZE"));
			col.setNullable(rs.getBoolean("NULLABLE"));
			col.setDigits(rs.getInt("DECIMAL_DIGITS"));
			col.setDefaultValue(rs.getString("COLUMN_DEF"));
			col.setComment(rs.getString("REMARKS"));
			col.setIsAutoIncrement("YES".equals(rs.getString("IS_AUTOINCREMENT")));
			
			table.getColumns().add(col);
		}
		close(null, null, rs);
	}

	/**
	 * 获取主键
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	protected void initPk(Table table) throws SQLException {

		DatabaseMetaData metaDdate = conn.getMetaData();
		ResultSet rs = metaDdate.getPrimaryKeys(null, null, table.getTableName());
		while (rs.next()) {
			String pkName  = rs.getString("COLUMN_NAME");
			List<Column> columns = table.getColumns();
			if(null == columns){
				initColumns(table);
			}else{
				for(Column col : columns){
					if(col.getName().equals(pkName)){
						col.setIsKey(true);
					}
				}
			}
			
		}
		close(null, null, rs);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Table> getTables() throws SQLException {
		this.connectionTest(conn);
		List<Table> tables = null;
		ResultSet rs = null;
		try {
			DatabaseMetaData dmd = conn.getMetaData();
			rs = dmd.getTables("", "", "%", null);
			tables = new ArrayList();
			while (rs.next()) {
				String tableName = rs.getString("TABLE_NAME");
				Table table  = this.getTable(tableName);
				tables.add(table);
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			close(conn, null, rs);
		}
		return tables;
	}


	@Override
	public Table getTable(String tableName) throws SQLException {
		connectionTest(conn);
		Table table = new Table(tableName);
		try {
			// 获取数据库的MataData信息
			initColumns(table);
			initPk(table);
		} catch (SQLException e) {
			 e.printStackTrace();;
		}catch (Exception e) {
			 e.printStackTrace();
		}
		finally {
			close(conn, null, null);
		}
		return table;
	}

}
